﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlsLibrary
{
    public partial class ControlTelephone : UserControl
    {
        private const string CityMask = "00-00-00";
        private const string MobileMask = "(000) 00-00-000";

        private string _telephoneValue = string.Empty;
        private TelephoneType _telephoneType = TelephoneType.City;

        private readonly int _height;

        [Browsable(true)]
        public event EventHandler TextBoxTextChanged;

        private void InvokeTextChanged()
        {
            var handler = TextBoxTextChanged;
            if (handler != null) 
                handler(this, new EventArgs());
        }

        public enum TelephoneType
        {
            Mobile,
            City
        }

        [Browsable(true)]
        [Description("Telephone value")]
        public string Telephone
        {
            set
            {
                _telephoneValue = value;
                maskedTextBox.Text = _telephoneValue;
            }
            get
            {
                _telephoneValue = maskedTextBox.Text;
                return _telephoneValue;
            }
        }

        [Browsable(true)]
        [Description("Telephone mask")]
        public TelephoneType TelephoneMask
        {
            set
            {
                _telephoneType = value;
                if (_telephoneType == TelephoneType.City)
                {
                    checkBox.Checked = true;
                    maskedTextBox.Mask = CityMask;
                }
                else
                {
                    checkBox.Checked = false;
                    maskedTextBox.Mask = MobileMask;
                }
            }
            get { return _telephoneType; }
        }

        public ControlTelephone()
        {
            InitializeComponent();
            _height = Height;
        }

        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            TelephoneMask = ((CheckBox)sender).Checked ? TelephoneType.City : TelephoneType.Mobile;
        }

//        private static string ClearTelephone(string telephone)
//        {
//            return telephone.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").ToLower();
//        }

        private void ControlTelephone_SizeChanged(object sender, EventArgs e)
        {
            Height = _height;
        }

        private void MaskedTextBoxTextChanged(object sender, EventArgs e)
        {
            InvokeTextChanged();
        }
    }
}
